<template>
  <div class="loginWrap">
    <div class="header">
      <van-nav-bar
        title="修改个人信息"
        left-text="返回"
        left-arrow
        fixed
        @click-left="onClickLeft"
      ></van-nav-bar>
    </div>
    <div class="contentWrap">
      <div class="content">
        <div class="loginWrap">
          <van-form @submit="submit">
            <van-field name="headPic" label="头像">
              <template #input>
                <van-uploader
                  v-model="headPic"
                  :after-read="afterRead"
                  :max-count="1"
                />
              </template>
            </van-field>
            <van-field
              v-model="username"
              name="用户名"
              label="用户名"
              placeholder="用户名"
              required
              disabled
              :clearable="true"
              :rules="[{ required: true, message: '请填写用户名' }]"
            />
            <van-field
              v-model="password"
              type="password"
              name="密码"
              label="密码"
              placeholder="密码"
              required
              disabled
              :clearable="true"
              :rules="[{ required: true, message: '请填写密码' }]"
            />
            <van-field
              v-model="phone"
              type="tel"
              name="手机号"
              label="手机号"
              placeholder="手机号"
              required
              :clearable="true"
              :rules="[{ required: true, message: '请填写手机号' }]"
            />
            <van-field
              v-model="email"
              name="邮箱"
              label="邮箱"
              placeholder="邮箱"
              required
              :clearable="true"
              :rules="[{ required: true, message: '请填写邮箱' }]"
            />
            <van-field
              v-model="college"
              name="学院"
              label="学院"
              placeholder="学院"
              required
              :clearable="true"
              :rules="[{ required: true, message: '请填写学院' }]"
            />
            <van-field name="role" label="角色" required>
              <template #input>
                <van-radio-group v-model="role" direction="horizontal">
                  <van-radio name="1">学生</van-radio>
                  <van-radio name="2">老师</van-radio>
                </van-radio-group>
              </template>
            </van-field>
            <van-field name="sex" label="性别" required>
              <template #input>
                <van-radio-group v-model="sex" direction="horizontal">
                  <van-radio name="1">男</van-radio>
                  <van-radio name="2">女</van-radio>
                </van-radio-group>
              </template>
            </van-field>
            <!-- <van-field
              v-model="safetyProblem1"
              name="安全问题1"
              label="安全问题1"
              placeholder="我的大学学号是？"
              required
              :clearable="true"
              :rules="[{ required: true, message: '请填写安全问题1' }]"
            />
            <van-field
              v-model="safetyAnswer1"
              name="安全问题答案1"
              label="答案1"
              placeholder="2017764423"
              required
              :clearable="true"
              :rules="[{ required: true, message: '请填写安全问题答案1' }]"
            />
            <van-field
              v-model="safetyProblem2"
              name="安全问题2"
              label="安全问题2"
              placeholder="我的大学名称是？"
              required
              :clearable="true"
              :rules="[{ required: true, message: '请填写安全问题2' }]"
            />
            <van-field
              v-model="safetyAnswer2"
              name="安全问题答案2"
              label="答案2"
              placeholder="岭南师范学院"
              required
              :clearable="true"
              :rules="[{ required: true, message: '请填写安全问题答案2' }]"
            /> -->
            <div style="margin: 16px">
              <van-button round block type="info" native-type="submit"
                >保存</van-button
              >
            </div>
          </van-form>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
// eslint-disable-next-line standard/object-curly-even-spacing
import { getUser, setUser } from "@/views/api/api.js";
import { Toast, Dialog } from "vant";
export default {
  components: {},
  data() {
    return {
      username: "", // 用户名
      password: "", // 用户密码
      phone: null, // 手机号
      email: "", // 邮箱
      id: "",
      college: "", // 学院
      // safetyProblem1: '', // 安全问题1
      // safetyProblem2: '', // 安全问题2
      // safetyAnswer1: '', // 安全问题答案1
      // safetyAnswer2: '', // 安全问题答案2
      sex: "1", // 性别 1男，2女
      role: "1", // 角色 1学生，2老师
      headPic: [], // 头像
      user: "",
    };
  },
  computed: {},
  created() {
    this.init();
  },
  methods: {
    init() {
      console.log("55455");
      this.user = JSON.parse(sessionStorage.getItem("user"));
      if (this.user[0]) {
        this.id = this.user[0]._id;
      }
      let params = {
        id: this.id,
      };
      getUser(params).then((res) => {
        if (!res.data.code) {
          // 查询成功
          this.headPic = [];
          if (res.data.Info[0].headPic) {
            this.headPic.push(res.data.Info[0].headPic);
          }
          this.username = res.data.Info[0].username;
          this.password = res.data.Info[0].password;
          this.phone = res.data.Info[0].phone;
          this.email = res.data.Info[0].email;
          this.college = res.data.Info[0].college;
          // this.safetyProblem1 = res.data.Info[0].safetyProblem1
          // this.safetyProblem2 = res.data.Info[0].safetyProblem2
          // this.safetyAnswer1 = res.data.Info[0].safetyAnswer1
          // this.safetyAnswer2 = res.data.Info[0].safetyAnswer2
          this.sex = res.data.Info[0].sex;
          this.role = res.data.Info[0].role === 1 ? "1" : "2";
        } else {
          // 查询失败
          Toast.fail(res.data.msg);
          this.$router.replace({ path: "/index" });
        }
      });
    },
    // 点击保存
    submit() {
      // 创建账号密码
      let params = {
        username: this.username,
        password: this.password,
        phone: this.phone,
        sex: this.sex,
        college: this.college,
        headPic: this.headPic,
        email: this.email,
        role: this.role,
        // safetyProblem1: this.safetyProblem1,
        // safetyProblem2: this.safetyProblem2,
        // safetyAnswer1: this.safetyAnswer1,
        // safetyAnswer2: this.safetyAnswer2
      };
      setUser(params).then((res) => {
        console.log(res);
        if (res.data.code) {
          Toast.fail(res.data.msg);
        } else {
          this.init();
          this.$store.commit("setUser", res.data.data);
          this.$store.commit("setToken", res.data.data);
          console.log("1111", res.data.data);
          Dialog.alert({
            message: "保存成功",
          }).then(() => {
            // this.$router.replace({ path: '/index' })
          });
        }
      });
    },
    // 点击返回
    onClickLeft() {
      this.$router.go(-1);
    },
    afterRead(file) {
      if (file.status === "uploading") {
        file.message = "上传中...";
      } else if (file.status === "failed") {
        file.message = "上传失败";
      } else {
        file.message = "上传成功";
      }
    },
  },
};
</script>
<style scoped>
.contentWrap {
  height: calc(100vh - 46px);
  overflow-y: auto;
}
/* 页面可以滚动，但是不出现滚动条 */
.contentWrap::-webkit-scrollbar {
  width: 0;
}
.header {
  height: 46px;
}
.content {
  margin: 0 16px;
}
.logo {
  width: 100%;
}
</style>
